package com.qf.consumer.service;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpRejectAndDontRequeueException;
import org.springframework.amqp.ImmediateAcknowledgeAmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * description
 *
 * @author TangLin
 * @date 2020年08月10日 16:19
 */
@Service
@Slf4j
//@RabbitListener(queues = {"queue1","queue2"})

public class ReceiveMsgService {

    @RabbitHandler
    @RabbitListener(queues = "queue1")
    public void receiveMsg(String msg){
        System.out.println("接收MSG："+msg);
        System.out.println(1/0);
//        throw new AmqpRejectAndDontRequeueException("");
    }
    @RabbitListener(queues = "dead_queue")
    public void process2(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("消费者获取生产者消息接收时间:" + simpleDateFormat.format(new Date()));

        // 获取消息Id
        String messageId = message.getMessageProperties().getMessageId();
        String msg = new String(message.getBody(), "UTF-8");
        log.info("死信消费者获取生产者消息msg:" + msg + ",消息id" + messageId);
        // // 手动ack
        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        // 手动签收
        channel.basicAck(deliveryTag, false);

        System.out.println("执行结束....");
    }


}
